package com.gzpi.hwgggh.controller;

import org.apache.commons.fileupload.DiskFileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping("/Upload")
public class UploadServlet extends HttpServlet {

    @RequestMapping(value = "/UploadImg.do",method = RequestMethod.POST)
    @ResponseBody
    public void UploadImg(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        FileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);
        // 开始读取上传信息
        int index = 0;
        List fileItems = null;

        try {
            fileItems = upload.parseRequest(request);
            System.out.println("fileItems=" + fileItems);
        } catch (Exception e) {
            e.printStackTrace();
        }

        Iterator iter = fileItems.iterator(); // 依次处理每个上传的文件
        while (iter.hasNext()) {
            FileItem item = (FileItem) iter.next();// 忽略其他不是文件域的所有表单信息
            if (!item.isFormField()) {
                String name = item.getName();// 获取上传文件名,包括路径
                String  fName = name.substring(0,name.lastIndexOf("/"));
             //   name = name.substring(name.lastIndexOf("/") + 1);// 从全路径中提取文件名
                long size = item.getSize();
                if ((name == null || name.equals("")) && size == 0)
                    continue;
                int point = name.indexOf(".");
                File file = new File("D:/图片/"+fName);
                if (!file .exists()  && !file .isDirectory()) {
                    file.mkdir();
                }
                File fNew = new File("D:/图片", name);
                try {
                    item.write(fNew);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            } else// 取出不是文件域的所有表单信息
            {
                String fieldvalue = item.getString();
                // 如果包含中文应写为：(转为UTF-8编码)
                // String fieldvalue = new
                // String(item.getString().getBytes(),"UTF-8");
            }
        }
        String text1 = "11";
        response.sendRedirect("result.jsp?text1=" + text1);
    }
}
